﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Diagnostics;

namespace Fayshine.DataTest
{
    public partial class Form1 : Form
    {
        private System.Threading.Timer m_Timer;
        private fayshineEntities m_Entity;
        private int m_index = 0;
        private int m_Duration = 0;

        private int m_Times = 0;

        public Form1()
        {
            InitializeComponent();
            button2.Enabled = false;
            button1.Enabled = true;
            m_Entity = new fayshineEntities();

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (comboBox1.SelectedIndex >= 0 && comboBox2.SelectedIndex >= 0)
            {
                listBox1.Items.Clear();
                m_index = comboBox1.SelectedIndex;
                if (comboBox2.SelectedIndex == 0)
                {
                    m_Duration = 1000;
                }
                else if (comboBox2.SelectedIndex == 1)
                {
                    m_Duration = 2 * 1000;
                }
                else if (comboBox2.SelectedIndex == 2)
                {
                    m_Duration = 5 * 1000;
                }


                m_Timer = new System.Threading.Timer(delegate(object state) { AutoInsertData(); }, null, 0, m_Duration);

                button2.Enabled = true;
                button1.Enabled = false;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (m_Timer != null)
            {
                m_Timer.Change(Timeout.Infinite, Timeout.Infinite);
            }
            button1.Enabled = true;
            button2.Enabled = false;

        }

        bool lastIn = false;
        private void AutoInsertData()
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new MethodInvoker(delegate { AutoInsertData(); }));
            }
            else
            {
                try
                {
                    if (m_index == 0)
                    {
                        lastIn = !lastIn;
                        Random random = new Random();
                        passengerflow a = new passengerflow();
                        a.DateTime = DateTime.Now.Subtract(new TimeSpan(0, 0, ++m_Times * 30));
                        a.Count = random.Next(1, 5);
                        a.GateID = random.Next(7, 13);
                        a.Gender = random.Next(0, 1) == 1 ? true : false;
                        a.IsIn = lastIn;
                        a.MaxAge = random.Next(1, 120);
                        a.MinAge = random.Next(1, a.MaxAge.Value);
                        int result;
                        if (m_Entity != null)
                        {
                            m_Entity.AddTopassengerflows(a);
                            result = m_Entity.SaveChanges();
                            m_Entity.AcceptAllChanges();
                            listBox1.Items.Add("Gate ID:" + a.GateID.ToString() + "; 进/出:" + a.IsIn.ToString() + "; 数量：" + a.Count.ToString() + "; 时间：" + a.DateTime.ToString());
                        }
                    }
                }
                catch (Exception ex)
                {

                }
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.comboBox1.SelectedIndex = 0;
            this.comboBox2.SelectedIndex = 0;
        }

        private void button3_Click(object sender, EventArgs e)
        {
        
            DateTime min = new DateTime(2012, 1, 29, 23, 59, 59);
            DateTime max = new DateTime(2012, 1, 30, 23, 59, 59);

            var result = m_Entity.StatisticByMinsAndDate(5, min,max);

            

            //IQueryable<Fayshine.DataTest.passengerflowbymin> query = result.AsQueryable<Fayshine.DataTest.passengerflowbymin>();
            //IEnumerable<Fayshine.DataTest.passengerflowbymin> byGate = 
            //    query.Select<Fayshine.DataTest.passengerflowbymin, Fayshine.DataTest.passengerflowbymin>(pf => pf.gateID == 8);

            IEnumerable<Fayshine.DataTest.passengerflowbymin> byGates = result.Where(pf => pf.isIn == true);

            foreach (Fayshine.DataTest.passengerflowbymin pf in byGates)
            {
                Debug.WriteLine(string.Format("GateID: {0}, DateTime: {1}, IsIn: {2}, count: {3}", pf.gateID, pf.dt, pf.isIn, pf.count));
            }
            //IEnumerable<Fayshine.DataTest.passengerflowbymin> byGates = records.Where(pf => pf.gateID == 8);

        }

    }
}
